//
//  WKWeiKeKaController.m
//  WeKeTang
//
//  Created by ichina on 2017/8/31.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKWeiKeKaController.h"
#import "ScottAlertController.h"
#import "NSString+KZExtension.h"
#import "WKLoginHandel.h"
#import "WKUniversal.h"
#import "WKNetworkTools.h"

@interface WKWeiKeKaController ()
@property (weak, nonatomic) IBOutlet UILabel *serialNumLabel;

@property (weak, nonatomic) IBOutlet UITextField *TFSerialNum;

@property (weak, nonatomic) IBOutlet UILabel *activationCodeLabel;
@property (weak, nonatomic) IBOutlet UITextField *TFActivationCode;

@property (weak, nonatomic) IBOutlet UIButton *btnActivation;

@end

@implementation WKWeiKeKaController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navItem.title = @"微课卡";
    
    [self setupUI];
}
- (void)setupUI{
    
    self.navgationBar.barTintColor = RGBColor(40, 188, 203);
    
    self.statusbar.backgroundColor = RGBColor(40, 188, 203);
    
    [self middleTitleColor:[UIColor whiteColor]];
    
    self.view.backgroundColor = RGBColor(245, 245, 245);
    
    [self setLeftButtonWithImage:@"icon_return_white" highlightedImage:@"icon_return_white" target:self action:@selector(backAction)];
    
    UIView *leftView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 47)];
    leftView1.contentMode = UIViewContentModeCenter;
    self.TFSerialNum.leftView = leftView1;
    self.TFSerialNum.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *leftView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 47)];
    leftView2.contentMode = UIViewContentModeCenter;
    self.TFActivationCode.leftView = leftView2;
    self.TFActivationCode.leftViewMode = UITextFieldViewModeAlways;
    
    self.TFSerialNum.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.TFActivationCode.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.serialNumLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    self.activationCodeLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    
    self.btnActivation.layer.cornerRadius = 3;
    self.btnActivation.layer.masksToBounds = YES;

}
- (void)backAction{
    if (self.weikekaBlock) {
        self.weikekaBlock();
    }
    [super backAction];
}
#pragma mark - 立即激活
- (IBAction)activationAction:(UIButton *)sender {
    
    if (StringIsNullOrEmpty(self.TFSerialNum.text)) {
        
        [self showCustomAlertMessage:@"请输入微课卡序列号"];
        return;
    }
    if (StringIsNullOrEmpty(self.TFActivationCode.text)) {
        
        [self showCustomAlertMessage:@"请输入微课卡激活码"];
        return;
    }
    __weak typeof(self) weakSelf = self;
    ScottAlertView *alertView = [ScottAlertView alertViewWithTitle:@"请您选择学段" message:@""];
    [alertView addAction:[ScottAlertAction actionWithTitle:@"初中" style:ScottAlertActionStyleCancel handler:^(ScottAlertAction *action) {
        [weakSelf activationActionWithPeriod:@"1021"];
    }]];
    
    [alertView addAction:[ScottAlertAction actionWithTitle:@"高中" style:ScottAlertActionStyleDestructive handler:^(ScottAlertAction *action) {
        [weakSelf activationActionWithPeriod:@"1022"];
    }]];
    
    ScottAlertViewController *alertController = [ScottAlertViewController alertControllerWithAlertView:alertView preferredStyle:ScottAlertControllerStyleAlert transitionAnimationStyle:ScottAlertTransitionStyleDropDown];
    alertController.tapBackgroundDismissEnable = YES;
    [self presentViewController:alertController animated:YES completion:nil];
   
}
- (void)activationActionWithPeriod:(NSString *)period{
    __weak typeof(self) weakSelf = self;
    NSString *userid = [WKLoginHandel getLoginDataWithString:@"userid"];
    NSString *sessid = [WKLoginHandel getLoginDataWithString:@"SESSID"];
    NSString *mac = [WKUniversal macaddress];
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:@"activemembercard" forKey:@"commandid"];
    [para setObject:userid forKey:@"userid"];
    [para setObject:self.TFSerialNum.text forKey:@"card"];
    [para setObject:self.TFActivationCode.text   forKey:@"code"];
    [para setObject:sessid forKey:@"phpsessid"];
    [para setObject:period forKey:@"period"];
    [para setObject:mac forKey:@"mac"];

    [[WKNetworkTools shareManger] setHttpMethod:@"POST"];
    [[WKNetworkTools shareManger]RunRequest:para path:kServerDomain tip:@"激活中..." completionHandler:^(id responseObject) {
        
    } errorHandler:^(NSError *error) {
        
    } finishHandler:^(id responseObject) {
        
        NSDictionary *dict = [responseObject mj_JSONObject];
        NSLog(@"激活码--------------%@",dict);
        NSDictionary *headDict = dict[@"head"];
        NSString *statuscode = headDict[@"statuscode"];
        NSString *msg = headDict[@"message"];
        if ([statuscode intValue] == 100) {
            
            [weakSelf showCustomAlertMessage:@"恭喜您！激活成功!"];
        }else{
            [weakSelf showCustomAlertMessage:msg];
        }
    }];
}

@end
